/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jfrozen.gameserver.ai.special;

import com.l2jfrozen.gameserver.managers.QuestManager;
import com.l2jfrozen.gameserver.model.L2Object;
import com.l2jfrozen.gameserver.model.L2Skill;
import com.l2jfrozen.gameserver.model.actor.instance.L2NpcInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2MonsterInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.quest.Quest;
import com.l2jfrozen.gameserver.model.quest.QuestState;
import com.l2jfrozen.gameserver.network.clientpackets.Say2;
import com.l2jfrozen.gameserver.network.serverpackets.CreatureSay;
import com.l2jfrozen.util.random.Rnd;

/**
 * @author Gnacik
 * @version 1.0
 * @see Warning! Mostly that event is custom!
 */
public class SquashEvent extends Quest implements Runnable
{
	//private static final int MANAGER = 31860;
	
	//private static final int NECTAR_SKILL = 2005;
	private static final int NECTAR_SKILL = 10000;
	
	private static final int[] CHRONO_LIST = 
	{
		4202, // Chrono Cithara
		5133, // Chrono Unitus
		//5817, // Chrono Campana
		7058, // Chrono Darbuka
		8350  // Chrono Maracas
	};
	
	private static final int[] SQUASH_LIST = 
	{
		12774, // Young Squash
		12775, // High Quality Squash
		12776, // Low Quality Squash
		12777, // Large Young Squash
		12778, // High Quality Large Squash
		12779, // Low Quality Large Squash
		13016, // King Squash
		13017  // Emperor Squash
	};
	
	private static final String[] _NOCHRONO_TEXT = 
	{
		"You cannot kill me without Chrono",
		"Hehe...keep trying...",
		"Nice try...",
		"Tired ?",
		"Go go ! haha..."
	};
	
	private static final String[] _CHRONO_TEXT = 
	{
		"Arghh... Chrono weapon...",
		"My end is coming...",
		"Please leave me !",
		"Heeellpppp...",
		"Somebody help me please..."
	};
	
	private static final String[] _NECTAR_TEXT = 
	{
		"Yummy... Nectar...",
		"Plase give me more...",
		"Hmmm.. More.. I need more...",
		"I will like you more if you give me more...",
		"Hmmmmmmm...",
		"My favourite..."
	};

	private static final int[][] DROPLIST =
	{
		// must be sorted by npcId !
		// npcId, itemId, chance
		// Young Squash
		/*
		{ 12774,  1060, 100 },	// Lesser Healing potion
		{ 12774,  1062,  50 },	// Haste potion
		// High Quality Squash
		{ 12775,  1539, 100 },	// Greater Healing potion
		{ 12775,  1375,  70 },	// Greater Swift Attack Potion
		{ 12775,  1459,  50 },	// Crystal c-grade
		// Low Quality Squash
		{ 12776,  1061, 100 },	// Healing potion
		{ 12776,  1062,  70 },	// Haste potion
		{ 12776,  1458,  50 },	// Crystal d-grade
		
		// Large Young Squash
		{ 12777,  1061, 100 },	// Healing potion
		{ 12777,  1374,  50 },	// Greater Haste potion
		// High Quality Large
		{ 12778,  1539, 100 },	// Greater Healing potion		
		{ 12778,  6036,  70 },	// Greater Magic Haste Potion
		{ 12778,  1459,  40 },	// Crystal c-grade
		// Low Quality Large
		{ 12779,  6035,  70 },	// Magic Haste Potion
		{ 12779,  1458,  50 },	// Crystal d-grade
		
		// King
		{ 13016,  1540, 100 },	// Quick Healing Potion
		{ 13016,  1460,  40 },	// Crystal b-grade
		{ 13016,  5234,  20 },	// Mystery Potion
		// Emperor
		{ 13017,  1540, 100 },	// Quick Healing Potion
		{ 13017, 20004,  40 },	// Energy Ginseng
		{ 13017,  1461,  20 },	// Crystal a-grade
		{ 13017,  5234,  10 },	// Mystery Potion
		{ 13017,  20392, 1 }	// Vitality potion
		*/
		// Young Squash
		{ 12774,  1060, 80 },	// Lesser Healing potion
		{ 12774,  1062,  30 },	// Haste potion
		{ 12774, 728, 25 },     // Mana Potion
		// High Quality Squash
		{ 12775,  1539, 90 },	// Greater Healing potion
		{ 12775,  1375,  70 },	// Greater Swift Attack Potion
		{ 12775, 6036, 70 },     // Greater Magic Haste Potion
		{ 12775,  1459,  60 },	// Crystal C-grade
		{ 12775, 728, 50 },  // Mana Potion
		//{ 12775, 4659, 2 }, // Blue Soul Crystal - Stage 8
		//{ 12775, 4648, 2 }, // Green Soul Crystal - Stage 8
		//{ 12775, 4637, 2 }, // Red Soul Crystal - Stage 8
		//{ 12775, 4658, 3 }, // Blue Soul Crystal - Stage 7
		//{ 12775, 4647, 3 }, // Green Soul Crystal - Stage 7
		//{ 12775, 4636, 3 }, // Red Soul Crystal - Stage 7
		{ 12775, 956, 20 },  // Scroll: Enchant Armor (Grade D)
		{ 12775, 955, 25 },  // Scroll: Enchant Weapon (Grade D)
		{ 12775, 952, 15 },  // Scroll: Enchant Armor (Grade C)
		{ 12775, 951, 20 },  // Scroll: Enchant Weapon (Grade C)
		{ 12775, 948, 10 },  // Scroll: Enchant Armor (Grade B)
		{ 12775, 947, 13 },  // Scroll: Enchant Weapon (Grade B)
		// Low Quality Squash
		{ 12776,  1061, 100 },	// Healing potion
		{ 12776,  1062,  70 },	// Haste potion
		{ 12776,  1458,  45 },	// Crystal D-grade
		{ 12776, 728 , 40 },    // Mana Potion
		{ 12776, 956, 20 },  // Scroll: Enchant Armor (Grade D)
		{ 12776, 955, 25 },  // Scroll: Enchant Weapon (Grade D)
		{ 12776, 952, 14 },  // Scroll: Enchant Armor (Grade C)
		{ 12776, 951, 18 },  // Scroll: Enchant Weapon (Grade C)
		//{ 12776, 4656, 3 },  // Blue Soul Crystal - Stage 5
		//{ 12776, 4645, 3 },  // Green Soul Crystal - Stage 5
		//{ 12776, 4634, 3 },  // Red Soul Crystal - Stage 5
		//{ 12776, 4657, 2 },  // Blue Soul Crystal - Stage 6
		//{ 12776, 4646, 2 },  // Green Soul Crystal - Stage 6
		//{ 12776, 4635, 2 },  // Red Soul Crystal - Stage 6
		// Large Young Squash
		{ 12777,  1061, 100 },	// Healing potion
		{ 12777,  1374,  70 },	// Greater Haste potion
		{ 12777, 728 , 60 },    // Mana Potion
		// High Quality Large
		{ 12778,  1539, 100 },	// Greater Healing potion		
		{ 12778,  6036,  70 },	// Greater Magic Haste Potion
		{ 12778,  1460,  30 },	// Crystal B-grade
		{ 12778,  1375,  35 },	// Greater Swift Attack Potion
		{ 12778,  948,  18 },   // Scroll: Enchant Armor (B)
		{ 12778,  952,  20 },	// Scroll: Enchant Armor (C)
		{ 12778,  951,  25 },	// Scroll: Enchant Weapon (C)
		{ 12778,  947,  13 },	// Scroll: Enchant Weapon (Grade B)
		{ 12778,  730,  9 },	// Scroll: Enchant Armor (Grade A)
		{ 12778, 728, 70 },    // Mana Potion
		//{ 12778, 4661, 6 },    // Blue Soul Crystal - Stage 10
		//{ 12778, 4650, 5 },    // Green Soul Crystal - Stage 10
		//{ 12778, 4639, 4 },    // Red Soul Crystal - Stage 10
		{ 12778, 5592, 80 },    // Great CP Potion
		// Low Quality Large
		{ 12779,  1539, 80 },	// Greater Healing potion
		{ 12779,  6035,  70 },	// Magic Haste Potion
		{ 12779,  1459,  50 },	// Crystal C-grade
		{ 12779, 728 , 50 },    // Mana Potion
		{ 12779, 956, 10 },  // Scroll: Enchant Armor (Grade D)
		{ 12779, 955, 20 },  // Scroll: Enchant Weapon (Grade D)
		{ 12779, 952, 15 },  // Scroll: Enchant Armor (Grade C)
		{ 12779, 951, 25 },  // Scroll: Enchant Weapon (Grade C)
		// King Squash
		{ 13016,  1540, 100 },	// Quick Healing Potion
		{ 13016,  6036,  50 },	// Greater Magic Haste Potion
		{ 13016,  1375,  60 },	// Greater Swift Attack Potion
		{ 13016,  1374,  75 },	// Greater Haste potion
		{ 13016,  1461,  40 },	// Crystal A-grade
		{ 13016,  5234,  20 },	// Mystery Potion
		{ 13016,  730,  9 },   // Scroll: Enchant Armor (A)
		{ 13016,  729,  5 },    // Scroll: Enchant Weapon (A)
		{ 13016,  1538,  11 },   // Blessed Scroll of Escape (Event)
		{ 13016,  947,  25 },	// Scroll: Enchant Weapon (Grade B)
		{ 13016,  730,  10 },	// Scroll: Enchant Armor (Grade A)
		{ 13016, 728 , 80 }, // Mana Potion
		// Emperor Squash
		{ 13017,  1540, 100 },	// Quick Healing Potion
		{ 13017,  6036,  60 },	// Greater Magic Haste Potion
		{ 13017,  1375,  70 },	// Greater Swift Attack Potion
		{ 13017,  1374,  80 },	// Greater Haste potion
		{ 13017,  1462,  65 },	// Crystal S-grade
		{ 13017,  5234,  9 },	// Mystery Potion
		{ 13017,  730,  50 },   // Scroll: Enchant Armor (A)
		{ 13017, 960,  5 },	    // Scroll: Enchant Armor (S)
		{ 13017,  729,  15 },    // Scroll: Enchant Weapon (A)
		{ 13017,  948,  45 },   // Scroll: Enchant Armor (B)
		{ 13017,  947,  40 },	// Scroll: Enchant Weapon (Grade B)
		{ 13017, 952, 100 },  // Scroll: Enchant Armor (Grade C)
		{ 13017, 951, 100 },  // Scroll: Enchant Weapon (Grade C)
		{ 13017,  3936,  50 },  // Blessed Scroll of Resurrection
		{ 13017,  1538,  50 },  // Blessed Scroll of Escape
		{ 13017,  1461,  50 },	// Crystal A-grade
		{ 13017, 728 , 100 }     // Mana Potion
	};
	
	public String onAttack(L2NpcInstance npc, L2PcInstance attacker, int damage, boolean isPet)
	{		
		if (contains(SQUASH_LIST,npc.getNpcId()))
		{
			if(isPet)
			{
				noChronoText(npc);
				npc.setIsInvul(true);				
				return null;
			}
			if(attacker.getActiveWeaponItem() != null && contains(CHRONO_LIST,attacker.getActiveWeaponItem().getItemId()))
			{
				ChronoText(npc);
				npc.setIsInvul(false);
				npc.getStatus().reduceHp(10, attacker);
				return null;
			}
			else
			{
				noChronoText(npc);
				npc.setIsInvul(true);
				return null;
			}
		}
		return super.onAttack(npc, attacker, damage, isPet);
	}
	
	//public String onSkillSee(L2NpcInstance npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
	public String onSkillUse(L2NpcInstance npc, L2PcInstance caster, L2Skill skill)
	{
        //if(contains(targets,npc) && contains(SQUASH_LIST,npc.getNpcId()) && (skill.getId() == NECTAR_SKILL))
		if(contains(SQUASH_LIST,npc.getNpcId()) && (skill.getId() == NECTAR_SKILL))
        {
			switch(npc.getNpcId())
        	{
        		case 12774:
                	randomSpawn(12775, 12776, npc, true);
        			break;
        		case 12777:
                	randomSpawn(12778, 12779, npc, true);
        			break;
        		case 12775:
       				randomSpawn(13016, npc, true);
        			break;
        		case 12778:
       				randomSpawn(13017, npc, true);
        			break;
        	}        	
        }
        //return super.onSkillSee(npc,caster,skill,targets,isPet);
		return super.onSkillUse(npc,caster,skill);
	}

    public String onKill(L2NpcInstance npc, L2PcInstance killer, boolean isPet) 
    { 
    	dropItem(npc, killer);
    	return super.onKill(npc, killer, isPet);
    }

    public String onSpawn(L2NpcInstance npc) 
    {
    	//npc.disableCoreAI(true);
		npc.setIsImobilised(true); 
    	return null;
    	// return super.onSpawn(npc);
    }
    
	private static final void dropItem(L2NpcInstance mob, L2PcInstance player)
	{
		final int npcId = mob.getNpcId();
		final int chance = Rnd.get(100);
		for (int i = 0; i < DROPLIST.length; i++)
		{
			int[] drop = DROPLIST[i];
			if (npcId == drop[0])
			{
				if (chance < drop[2])
				{
					if(drop[1] > 20000)
						((L2MonsterInstance)mob).DropItem(player, drop[1], 2);
					else
						((L2MonsterInstance)mob).DropItem(player, drop[1], Rnd.get(2, 6));
					continue;
				}
			}
			if (npcId < drop[0])
				return; // not found
		}
	}
	
	private void randomSpawn(int lower, int higher, L2NpcInstance npc, boolean delete)
	{
		int _random = Rnd.get(100);
		if (_random < 10)
			spawnNext(lower, npc);
		else if(_random < 30)
			spawnNext(higher, npc);
		else
			nectarText(npc);
	}
	
	private void randomSpawn(int npcId, L2NpcInstance npc, boolean delete)
	{
		if(Rnd.get(100) < 10)
			spawnNext(npcId, npc);
		else
			nectarText(npc);
	}
	
	private void ChronoText(L2NpcInstance npc)
	{
		if(Rnd.get(100) < 20)
			npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _CHRONO_TEXT[Rnd.get(_CHRONO_TEXT.length)]));		
	}
	private void noChronoText(L2NpcInstance npc)
	{
		if(Rnd.get(100) < 20)
			npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _NOCHRONO_TEXT[Rnd.get(_NOCHRONO_TEXT.length)]));		
	}
	private void nectarText(L2NpcInstance npc)
	{
		if(Rnd.get(100) < 30)
			npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _NECTAR_TEXT[Rnd.get(_NECTAR_TEXT.length)]));
	}
	
    private void spawnNext(int npcId, L2NpcInstance npc)
    {
    	addSpawn(npcId, npc.getX(), npc.getY(), npc.getZ(), npc.getHeading(), false, 60000);		
    	npc.deleteMe();
    }

	public static <T> boolean contains(T[] array, T obj)
	{
		for (int i = 0; i < array.length; i++)
		{
			if (array[i] == obj)
			{
				return true;
			}
		}
		return false;
	}
	
	public static boolean contains(int[] array, int obj)
	{
		for (int i = 0; i < array.length; i++)
		{
			if (array[i] == obj)
			{
				return true;
			}
		}
		return false;
	}
	
	public SquashEvent(int questId, String name, String descr)
	{
		super(questId, name, descr);
		
		for (int mob : SQUASH_LIST)
		{
			addAttackId(mob);
			addKillId(mob);
			addSpawnId(mob);
			//addSkillSeeId(mob);
		}

		//addStartNpc(MANAGER);
		//addFirstTalkId(MANAGER);
		//addTalkId(MANAGER);
		//addSpawn(MANAGER, 83077, 147910, -3471, 29412, false, 0);
	}

	@Override
	public String onFirstTalk(L2NpcInstance npc, L2PcInstance player)
	{
		String htmltext = "";
		QuestState st = player.getQuestState(getName());
		if (st == null)
		{
			Quest q = QuestManager.getInstance().getQuest(getName());
			st = q.newQuestState(player);
		}
		htmltext = npc.getNpcId() + ".htm";
		return htmltext;
	}
	
    /*public static void main(String[] args)
    {
		new SquashEvent(-1,"SquashEvent","events");
    }*/
	
	@Override
	public void run()
	{}
}